<?php
namespace app\admin\model;

/*用户信息模型*/
use app\admin\model\CommonModel;

class MaliceIp extends CommonModel
{
    protected $table_name = 'malice_ip';

    public function all($args)
    {
        /**
         * $args参数说明
         * args.client_id 根据用户client_id
         * args.keyword 根据 name phone company与之匹配
         * args.start_time
         * args.end_time
         */

        $pageLimit = isset($args['pageLimit']) && intval($args['pageLimit']) > 0 ? $args['pageLimit'] : 15;
        $where = [];
        if(isset($args['keyword']) && !empty($args['keyword']))
        {
            $where['ip'] = ['like','%'.$args['keyword'].'%'];
        }
        //获取数据
        $data = null;
        if(!empty($where))
        {
            $data = db($this->table_name)->where($where)->order('id DESC')->paginate($pageLimit);
        }
        else
        {
            $data = db($this->table_name)->order('id DESC')->paginate($pageLimit);
        }
        if(!empty($data))
            return [
                'data' => $data,
                'page' => $data->render()
            ];
    }

    //添加
    public function add($iplist)
    {
        $iplist = str_replace("，",",",$iplist);
        $ip = explode(",",$iplist);
        $data = [];
        if(empty($ip)) return false;
        foreach($ip as $k=>$v)
        {
           $data[] = ['ip'=>trim($v)];
        }
        db($this->table_name)->insertAll($data);
        return true;
    }

    public function del($args)
    {
        /**
         * args参数说明
         * args为一个数组，包含所有的id值
         */
        if(empty($args))
        {
            return $this->returnState(1,"没有记录被删除");
        }
        db($this->table_name)->where(['id'=>['in',implode(',',$args)]])->delete();
        return $this->returnState(0,"删除完毕");
    }
}